package com.idelan.commonMessage.controller;

import com.idelan.commonMessage.constant.ResponseCode;
import com.idelan.commonMessage.constant.RestApi;
import com.idelan.commonMessage.model.Mail163Parameter;
import com.idelan.commonMessage.model.MailQQParameter;
import com.idelan.commonMessage.utils.Mail163Util;
import com.idelan.commonMessage.utils.MailQQUtil;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.validation.Valid;

/**
* @Description:    java类作用描述
* @Author:         jzhou
* @CreateDate:     2018\7\18 0018 17:02
* @UpdateUser:     jzhou
* @UpdateDate:     2018\7\18 0018 17:02
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@RestController
@RequestMapping(value = "/mail")
@Api("邮件发送")
public class MailController {
    @PostMapping(value="/send163Mail")
    public RestApi sendMail(@Valid Mail163Parameter mail163Parameter, BindingResult bindingResult) throws MessagingException {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return mail163Parameter != null ? RestApi.getSuccessApi(
                Mail163Util.sendMail(
                mail163Parameter.getUser(),
                mail163Parameter.getPassword(),
                mail163Parameter.getSender(),
                mail163Parameter.getReceived(),
                mail163Parameter.getSubject(),
                mail163Parameter.getContent(),
                mail163Parameter.getContentType())) : null;
    }
    @PostMapping(value="/sendqqMail")
    public RestApi sendMail(@Valid MailQQParameter mailqqParameter, BindingResult bindingResult) throws MessagingException {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return mailqqParameter != null && null!=mailqqParameter.getRecipients()? RestApi.getSuccessApi(
                MailQQUtil.sendEmail(
                        mailqqParameter.getLoginAccount(),
                        mailqqParameter.getLoginAuthCode(),
                        mailqqParameter.getSender(),
                        mailqqParameter.getRecipients(),
                        mailqqParameter.getEmailSubject(),
                        mailqqParameter.getEmailContent(),
                        mailqqParameter.getEmailContentType())) : null;
    }
}
